<template>
  <div>
    <operation-title
      title="浮动按钮"
      :icon="TitleIcon"
    />
    <div class="divider"></div>
    <div class="func">
      <p class="attr">图标</p>
      <el-radio-group v-model="config.pageConfig.style" class="ml-4">
        <el-radio label="1">图标</el-radio>
        <el-radio label="2">图片</el-radio>
      </el-radio-group>
    </div>
    <div class="divider"></div>
    <div class="func" v-if="config.pageConfig.style == '1'">
      <p class="attr">组件风格</p>
      <ul class="common-ul">
        <li @click="config.pageConfig.custom = '1'" :class="config.pageConfig.custom == '1' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/stylesolid.png" alt="" />
        </li>
        <li @click="config.pageConfig.custom = '2'" :class="config.pageConfig.custom == '2' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/styleline.png" alt="" />
        </li>
      </ul>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">组件位置</p>
      <el-radio-group v-model="config.pageConfig.position" class="ml-4">
        <el-radio label="1">左上角</el-radio>
        <el-radio label="2">右上角</el-radio>
        <el-radio label="3">左下角</el-radio>
        <el-radio label="4">右下角</el-radio>
      </el-radio-group>
    </div>
    <div class="divider"></div>
    <div class="func" v-if="config.pageConfig.style == '1'">
      <p class="attr">选择颜色</p>
      <div class="adjust-item">
        <span>颜色</span>
        <el-color-picker v-model="config.pageConfig.color" />
        <span>{{ config.pageConfig.color }}</span>
      </div>
      <div class="adjust-item">
        <span>透明度</span>
        <el-slider v-model="config.pageConfig.opacity" :min="0" :max="100" size="small" />
        <span>{{ config.pageConfig.opacity }}%</span>
      </div>
    </div>
    <div class="divider"></div>

    <div class="func">
      <!-- <h4 v-if="config.pageConfig.style == '2'">建议图片40px*40px。</h4> -->
      <ul v-if="config.pageConfig.imgList.length > 0" class="add-item">
        <li v-for="(item, index) in config.pageConfig.imgList" :key="index">
          <div class="add-item-input">
            <!-- <el-icon v-if="config.pageConfig.style == '1'">
              <component :is="item.iconUrl"></component>
            </el-icon> -->
            <span @click="showDialog(index)" v-if="config.pageConfig.style == '1'" class="iconfont"
              :class="'icon-' + item.iconUrl"></span>

            <!-- <img
              v-else-if="config.pageConfig.style == '2' && item.imgUrl"
              :src="item.imgUrl"
              alt=""
            />
            <img
              v-else-if="config.pageConfig.style == '2' && !item.imgUrl"
              src="@/assets/app/bar/default_picture.png"
              alt=""
            /> -->

            <div class="upload-img" v-else-if="config.pageConfig.style == '2' && item.imgUrl">
              <el-image style="width: 100%; height: 100%" :src="item.imgUrl" fit="contain"></el-image>
              <div class="upload-img_cover">
                <Upload :model-value="fileList" file-path="act/" :limit="999" :show-file-list="false"
                  @success="uploadSuccess">
                  <el-button @click="currentIndex = index" size="small">替换</el-button>
                </Upload>
              </div>
            </div>
            <Upload v-else-if="config.pageConfig.style == '2' && !item.imgUrl" v-model="item.imgUrl" file-path="act/"
              list-type="picture-card">
              <el-icon>
                <Plus />
              </el-icon>
            </Upload>
            <div class="add-item-link" v-if="item.type == 'link'" @click="linkDialog(item, index)">
              <el-icon>
                <Link />
              </el-icon>
              <span v-if="item.url">{{ item.urlName }}</span>
              <span v-else>请选择链接</span>
              <!-- <el-input v-model="item.url" placeholder="请填写带有https/http的链接地址" clearable></el-input> -->
              <!-- <span v-if="item.name">请选择链接</span>
              <span v-else>请选择链接</span>
              <el-icon v-if="item.name" class="close-btn"><Close /></el-icon> -->
            </div>
            <div class="add-item-link" v-else-if="item.type == 'top'">
              返回顶部
            </div>
            <div class="add-item-link" v-else-if="item.type == 'service'">
              小程序客服
            </div>
          </div>
          <el-popconfirm confirm-button-text="是" cancel-button-text="否" @confirm="sliceItem(index)" :title="config.pageConfig.imgList.length > 1
              ? '确定删除吗？'
              : '只有一个不可删除'
            ">
            <template #reference>
              <el-icon class="sure-close">
                <Close />
              </el-icon>
            </template>
          </el-popconfirm>
        </li>
        <li v-if="config.pageConfig.imgList.length < 4" class="extra-li">
          <div @click="addItem('link')">+添加链接</div>
          <div @click="addItem('top')">+添加返回顶部</div>
          <div @click="addItem('service')">+添加小程序客服</div>
        </li>
      </ul>
    </div>
    <div class="divider"></div>

    <div class="func">
      <p class="attr">组件边距</p>
      <div class="adjust-item">
        <span>图标边距</span>
        <el-slider v-model="config.pageConfig.iconmargin" :min="0" :max="50" show-input size="small" />px
      </div>
      <div class="adjust-item">
        <span>距离底部</span>
        <el-slider v-model="config.pageConfig.marginBottom" :min="0" :max="50" show-input size="small" />px
      </div>
      <div class="adjust-item" v-if="['2', '4'].includes(config.pageConfig.position)">
        <span>距离右侧</span>
        <el-slider v-model="config.pageConfig.marginright" :min="0" :max="50" show-input size="small" />px
      </div>
      <div class="adjust-item" v-if="['1', '3'].includes(config.pageConfig.position)">
        <span>距离左侧</span>
        <el-slider v-model="config.pageConfig.marginleft" :min="0" :max="50" show-input size="small" />px
      </div>
    </div>
    <div class="divider"></div>

    <div class="func">
      <p class="attr">组件大小</p>
      <div class="adjust-item">
        <span>宽</span>
        <el-slider v-model="config.pageConfig.width" :min="0" :max="200" show-input size="small" />px
      </div>
      <div class="adjust-item">
        <span>高</span>
        <el-slider v-model="config.pageConfig.height" :min="0" :max="200" show-input size="small" />px
      </div>
    </div>
    <div class="divider"></div>

    <icon-selector
      v-model:show="dialogFormVisible"
      @select="(value) => config.pageConfig.imgList[index].iconUrl = value"
    />

    <href-selector
      v-model:show="linkFormVisible"
      :model-value="linkObject"
      @confirm="(link) => submitUrl(link, config.pageConfig.imgList[linkIndex])"
    />

  </div>
</template>

<script lang="ts">
export default {
  name: "FloatbtnOperation",
};
</script>
<script lang="ts" setup>
import { reactive, onMounted } from "vue";
import {useHrefSelector} from "@/views/activity/actApp/components/default/compositions/useHrefSelector";
import TitleIcon from "@/assets/app/icon/float.png";
import OperationTitle
  from "@/views/activity/actApp/components/default/operationComponents/operationTitle/OperationTitle.vue";
const config = defineProps({
  pageConfig: {
    type: Object,
    default: {
      style: "1",
      custom: "1",
      position: "4",
      color: "#FD463E",
      opacity: 100,
      marginright: 20,
      marginleft: 20,
      marginBottom: 50,
      width: 100,
      height: 100,
      iconmargin: 15,
      imgList: [
        {
          type: "link",
          iconUrl: "31dianhua",
          imgUrl: "",
          name: "",
          url: "",
          urlType: '',
          urlName: '',
        },
      ],
    },
  },
});
const sliceItem = (idx: Number) => {
  if (config.pageConfig.imgList.length < 2) {
    return;
  } else {
    config.pageConfig.imgList.splice(idx, 1);
  }
};
const currentIndex = ref(999);
const fileList = ref([]);
const uploadSuccess = (value: any) => {
  config.pageConfig.imgList[currentIndex.value].imgUrl = value.url;
};
const addItem = (idx: string) => {
  if (config.pageConfig.imgList.length > 3) {
    return;
  } else {
    console.log(idx);
    config.pageConfig.imgList.push({
      type: idx,
      iconUrl: idx == "link" ? "31dianhua" : idx == "top" ? "icon" : "kefu",
      imgUrl: "",
      name: "",
      url: "",
      urlType: '',
      urlName: '',
    });
  }
};
let index = 0;
let emits = defineEmits<{ (e: "changeStyle", configStyle: Object): void }>();

const {linkFormVisible, linkIndex, linkObject, submitUrl, linkDialog} = useHrefSelector();

const dialogFormVisible = ref(false);
const resetForm = () => {
  dialogFormVisible.value = false;
};
const showDialog = (idx: number) => {
  index = idx;
  dialogFormVisible.value = true;
};
const submitForm = (item: string) => {
  config.pageConfig.imgList[index].iconUrl = item;
  dialogFormVisible.value = false;
};

watch(config, (val) => {
  emits("changeStyle", config.pageConfig);
});
</script>

<style lang="scss" scoped>
.title {
  padding: 8px 23px;
  overflow: hidden;
  border-bottom: 5px solid #f6f7f9;
  display: flex;
  position: relative;
  height: 50px;
  box-sizing: border-box;

  img {
    position: absolute;
    left: 0;
    top: -5px;
  }

  h5 {
    font-size: 16px;
    font-weight: 700;
    position: absolute;
    left: 70px;
    top: 0;
    line-height: 50px;
  }
}

.func {
  border-bottom: 5px solid #f6f7f9;
  padding: 20px 16px;
  font-size: 16px;

  .attr {
    line-height: 1;
    color: #47565d;
    font-weight: bolder;
    margin-bottom: 20px;
  }

  ul.common-ul {
    list-style: none;
    display: flex;
    align-items: center;
    justify-content: center;

    li {
      flex: 1;
      height: 30px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 4px 0 0 4px;
      border: 1px solid #ededed;
      border-collapse: collapse;

      &.active {
        border: 1px solid #fb6638;
      }

      img {
        max-width: 100% !important;
        max-height: 100% !important;
      }
    }
  }

  h4 {
    position: relative;
    padding: 8px 16px 8px 16px;
    border-radius: 6px;
    color: red;
    font-weight: 600;
    font-size: 16px;
    line-height: 22px;
    margin-bottom: 10px;
    border: 1px solid #d8e8fc;
    background-color: #e5f0fe;
  }

  div.adjust-item {
    display: flex;
    align-items: center;
    margin-top: 3px;
    margin-bottom: 10px;

    span {
      width: 70px;
      text-align: right;
      margin-right: 15px;
      color: #666;
    }

    :deep(.el-slider) {
      width: 240px;
      margin-right: 3px;
      margin-left: 15px;
    }

    :deep(.el-slider__runway.show-input) {
      margin-right: 15px;
    }

    :deep(.el-input-number) {
      width: 100px;
      background: #f4f3f7;
      border: 1px solid #f4f3f7;
    }
  }

  .add-item {
    width: 100%;
    list-style: none;

    li {
      background: #f4f3f7;
      border-radius: 4px;
      padding: 10px 20px 10px 10px;
      color: #999;
      margin-top: 10px;
      margin-bottom: 20px;
      position: relative;

      .add-item-input {
        display: flex;
        align-items: center;
        margin-bottom: 20px;

        >p {
          width: 50px;
          margin-right: 10px;
        }

        > :deep(.el-icon) {
          font-size: 40px;
          margin-right: 10px;
        }

        .iconfont {
          font-size: 40px;
          margin-right: 10px;
        }

        .upload-img {
          width: 50px;
          height: 50px;
          margin-right: 10px;
          position: relative;

          :deep(.el-image) {
            width: 50px;
            height: 50px;
          }

          &:hover {
            .upload-img_cover {
              opacity: 1;
            }
          }

          .upload-img_cover {
            opacity: 0;
            position: absolute;
            transition: all 0.3s ease;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.7);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-evenly;
          }
        }

        :deep(.el-upload-list) {
          width: 50px;
          height: 50px;
          margin-right: 10px;
        }

        :deep(.el-upload--picture-card) {
          width: 50px;
          height: 50px;
        }

        :deep(.el-color-picker) {
          margin-right: 10px;
        }
      }

      .add-item-link {
        width: calc(100% - 80px);
        height: 30px;
        display: flex;
        align-items: center;
        background: #e8effc;
        border: 0;
        padding-left: 16px;
        cursor: pointer;
        position: relative;

        :deep(.el-icon) {
          margin-right: 10px;
        }

        :deep(.el-input) {
          font-size: 12px;
          height: 30px;
          width: calc(100% - 30px);
        }
      }

      .sure-close {
        position: absolute;
        right: 5px;
        top: 5px;
        cursor: pointer;
      }
    }

    .extra-li {
      display: flex;

      div {
        width: 33%;
        margin-right: 10px;
        border: 1px dashed #6b7685;
        line-height: 32px;
        height: 32px;
        border-radius: 4px;
        text-align: center;
        cursor: pointer;
        font-size: 13px;
      }
    }
  }
}

:deep(.el-dialog) {
  .icon-div-container {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
  }

  div.icon-div {
    width: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;
    border: 1px solid #47565d;
    margin: 10px;

    span {
      font-size: 30px;
    }
  }
}

:deep(.el-radio__label) {
  font-size: 16px;
}
</style>
